package com.example.mountainwalker.mapper;

import com.example.mountainwalker.entity.UserFollowInfo;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface UserFollowMapper {
    // 查询所有记录（包含用户名和被关注用户名）
    @Select("SELECT f.follow_id as followId, f.user_id as userId, f.followed_user_id as followedUserId, " +
            "f.followed_time as followedTime, u1.user_name as userName, u2.user_name as followedUserName " +
            "FROM user_follow f " +
            "LEFT JOIN user_info u1 ON f.user_id = u1.user_id " +
            "LEFT JOIN user_info u2 ON f.followed_user_id = u2.user_id")
    List<UserFollowInfo> getUserFollowList();

    // 按条件查询
    @Select("<script>" +
            "SELECT f.follow_id as followId, f.user_id as userId, f.followed_user_id as followedUserId, " +
            "f.followed_time as followedTime, u1.user_name as userName, u2.user_name as followedUserName " +
            "FROM user_follow f " +
            "LEFT JOIN user_info u1 ON f.user_id = u1.user_id " +
            "LEFT JOIN user_info u2 ON f.followed_user_id = u2.user_id " +
            "<where>" +
            "   <if test='username != null'> AND u1.user_name LIKE CONCAT('%', #{username}, '%') </if>" +
            "   <if test='followedUsername != null'> AND u2.user_name LIKE CONCAT('%', #{followedUsername}, '%') </if>" +
            "   <if test='startTime != null'> AND f.followed_time &gt;= #{startTime} </if>" +
            "   <if test='endTime != null'> AND f.followed_time &lt;= #{endTime} </if>" +
            "</where>" +
            "</script>")
    List<UserFollowInfo> getUserFollowListByCondition(
            @Param("username") String username,
            @Param("followedUsername") String followedUsername,
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime);

    // 按主键查询
    @Select("SELECT f.follow_id as followId, f.user_id as userId, f.followed_user_id as followedUserId, " +
            "f.followed_time as followedTime, u1.user_name as userName, u2.user_name as followedUserName " +
            "FROM user_follow f " +
            "LEFT JOIN user_info u1 ON f.user_id = u1.user_id " +
            "LEFT JOIN user_info u2 ON f.followed_user_id = u2.user_id " +
            "WHERE f.follow_id = #{followId}")
    UserFollowInfo getUserFollowById(@Param("followId") Integer id);

    // 按主键删除
    @Delete("DELETE FROM user_follow WHERE follow_id = #{followId}")
    int deleteUserFollowById(@Param("followId") Integer id);

    // 批量删除
    @Delete("<script>" +
            "DELETE FROM user_follow WHERE follow_id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchDeleteUserFollow(@Param("ids") List<Integer> ids);

    // 插入记录
    @Insert("INSERT INTO user_follow(follow_id, user_id, followed_user_id, followed_time) " +
            "VALUES(#{followId}, #{userId}, #{followedUserId}, #{followedTime})")
    int addUserFollow(UserFollowInfo userFollowInfo);

    // 更新记录
    @Update("UPDATE user_follow SET " +
            "user_id = #{userId}, " +
            "followed_user_id = #{followedUserId}, " +
            "followed_time = #{followedTime} " +
            "WHERE follow_id = #{followId}")
    int updateUserFollow(UserFollowInfo userFollowInfo);
}